package com.eh.taxation.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eh.commonutils.JwtUtils;
import com.eh.commonutils.R;
import com.eh.commonutils.vo.UcenterMemberOrder;
import com.eh.taxation.client.UcenterClient;
import com.eh.taxation.entity.Comment;
import com.eh.taxation.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author erhuo
 * @since 2021-01-21
 */
@RestController
@RequestMapping("/taxation/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    // 分页查询评论
    @PostMapping("/getCourseCommentPageList/{courseId}/{page}/{limit}")
    public R getCourseCommentPageList(@PathVariable long page,
                                      @PathVariable long limit,
                                      @PathVariable String courseId) {
        Page<Comment> pageParam = new Page<>(page, limit);
        Map<String, Object> map = commentService.getCourseCommentPageList(pageParam, courseId);
        return R.ok().data(map);
    }

    // 添加评论
    @PostMapping("/addCourseComment")
    public R addCourseComment(HttpServletRequest request, @RequestBody Comment courseComment) {
        String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberOrder member = ucenterClient.getUserInfoCourse(jwtToken);
        courseComment.setMemberId(jwtToken);
        courseComment.setNickname(member.getNickname());
        courseComment.setAvatar(member.getAvatar());
        System.out.println(courseComment);
        commentService.save(courseComment);
        return R.ok();
    }

    // 删除评论
    @DeleteMapping("deleteComment/{id}")
    public R deleteComment(@PathVariable String id, HttpServletRequest request) {
        String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        if (StringUtils.isEmpty(jwtToken)) return R.error().message("请先登录");
        commentService.removeById(id);
        return R.ok();
    }

    // 查询memberid
    @GetMapping("getMemberId")
    public R getMemberId(HttpServletRequest request) {
        String jwtToken = JwtUtils.getMemberIdByJwtToken(request);
        return R.ok().data("memberId", jwtToken);
    }



}

